package ui;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;
import java.util.List;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.wb.swt.SWTResourceManager;

import Crawler_data.Crawler_data;
import each_class.SongClass;

//搜索栏
//
public class SearchBar extends Composite {
	private Display display;
	private Text text;
	private static int offset = 0;
	private static String sear;
	public SearchBar(Composite parent, int style) {
		super(parent, style);
		display = getDisplay();
		setSize(290, 42);
		setBackground(SWTResourceManager.getColor(230, 230, 230));
		setBackgroundImage(SWTResourceManager.getImage(SearchBar.class, "/image/searchBar_background.png"));

		// 文本框
		text = new Text(this, SWT.NONE);
		text.setVisible(true);
		text.setMessage("🔍搜索音乐");
		text.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 16, SWT.NORMAL));
		text.setBackground(SWTResourceManager.getColor(230, 230, 230));
		text.setBounds(20, 5, 230, 32);
		text.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				if (e.keyCode == 13) {
					String search = text.getText();
					if (search != null) {
						search(search);
					}
				}
			}
		});

		// 搜索按钮
		Button searchButton = new Button(this, SWT.NONE) {

			@Override
			public void mouseUp(MouseEvent e) {
				String search = text.getText();
				if (search != null) {
					search(search);
				}
			}
		};
		searchButton.setBounds(250, 5, 32, 32);
		searchButton.initial(SWTResourceManager.getImage(SearchBar.class, "/image/search_button_deactive.png"),
				SWTResourceManager.getImage(SearchBar.class, "/image/search_button_active.png"));
		searchButton.setVisible(false);

		text.addFocusListener(new FocusListener() {

			@Override
			public void focusLost(FocusEvent e) {
				if (text.getText() == null) {
					searchButton.setVisible(false);

				}
			}

			@Override
			public void focusGained(FocusEvent e) {
				searchButton.setVisible(true);
			}
		});
	}

	public void setSearchText(String search ) {
		text.setText(search);
	}
	
	public String getSearchText() {
		return text.getText();
	}
	
	// 显示搜索页面UIshix
	public void search(String search) {
		// TODO显示搜索中界面
		new Thread() {
			public void run() {
				List<SongClass> result = searchData(search);
				display.asyncExec(new Runnable() {

					@Override
					public void run() {
						ui.global.Current.getSearchUI().initail(result);
						ui.global.Current.getUI().setVisible(false);
						ui.global.Current.setUI(ui.global.Current.getSearchUI());
						if(LeftButton.currentButton != null) {
							LeftButton.currentButton.deactive();
							LeftButton.currentButton = null;
						}
						ui.global.Current.getUI().setVisible(true);
					}
				});
			};
		}.start();
	}

	// 继续搜索
	public List<SongClass> searchMore() {
		// 外部已开线程
		offset += 24;
		return Crawler_data.grabble(sear, offset);
	}

	protected List<SongClass> searchData(String search) {

		sear = search;
		offset = 0;
		return Crawler_data.grabble(search, offset);
	}
}
